package com.turnengine.client.local.action.command;

import com.turnengine.client.local.action.enums.ActionTargetType;
import com.turnengine.client.local.unit.enums.UnitRelation;
import com.turnengine.client.local.unit.enums.UnitType;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The Add Action Target.
 */
public class AddActionTarget implements IAddActionTarget {

	/** The command id. */
	public static final short COMMAND_ID = 1002;

	/** The login id. */
	private long loginId;
	/** The instance id. */
	private int instanceId;
	/** The action id. */
	private int actionId;
	/** The target. */
	private ActionTargetType target;
	/** The type. */
	private UnitType type;
	/** The relation. */
	private UnitRelation relation;

	/**
	 * Creates a new Add Action Target.
	 */
	public AddActionTarget() {
	}

	/**
	 * Creates a new Add Action Target.
	 * @param loginId the login id
	 * @param instanceId the instance id
	 * @param actionId the action id
	 * @param target the target
	 * @param type the type
	 * @param relation the relation
	 */
	public AddActionTarget(long loginId, int instanceId, int actionId, ActionTargetType target, UnitType type, UnitRelation relation) {
		setLoginId(loginId);
		setInstanceId(instanceId);
		setActionId(actionId);
		setTarget(target);
		setType(type);
		setRelation(relation);
	}

	/**
	 * Creates a new Add Action Target.
	 * @param iAddActionTarget the i add action target
	 */
	public AddActionTarget(IAddActionTarget iAddActionTarget) {
		setLoginId(iAddActionTarget.getLoginId());
		setInstanceId(iAddActionTarget.getInstanceId());
		setActionId(iAddActionTarget.getActionId());
		setTarget(iAddActionTarget.getTarget());
		setType(iAddActionTarget.getType());
		setRelation(iAddActionTarget.getRelation());
	}

	/**
	 * Returns the login id.
	 * @return the login id.
	 */
	public long getLoginId() {
		return loginId;
	}

	/**
	 * Returns the instance id.
	 * @return the instance id.
	 */
	public int getInstanceId() {
		return instanceId;
	}

	/**
	 * Returns the action id.
	 * @return the action id.
	 */
	public int getActionId() {
		return actionId;
	}

	/**
	 * Returns the target.
	 * @return the target.
	 */
	public ActionTargetType getTarget() {
		return target;
	}

	/**
	 * Returns the type.
	 * @return the type.
	 */
	public UnitType getType() {
		return type;
	}

	/**
	 * Returns the relation.
	 * @return the relation.
	 */
	public UnitRelation getRelation() {
		return relation;
	}

	/**
	 * Sets the login id.
	 * @param loginId the login id to set.
	 */
	public void setLoginId(long loginId) {
		this.loginId = loginId;
	}

	/**
	 * Sets the instance id.
	 * @param instanceId the instance id to set.
	 */
	public void setInstanceId(int instanceId) {
		this.instanceId = instanceId;
	}

	/**
	 * Sets the action id.
	 * @param actionId the action id to set.
	 */
	public void setActionId(int actionId) {
		this.actionId = actionId;
	}

	/**
	 * Sets the target.
	 * @param target the target to set.
	 */
	public void setTarget(ActionTargetType target) {
		if (target == null) {
			throw new NullPointerException("target");
		}
		this.target = target;
	}

	/**
	 * Sets the type.
	 * @param type the type to set.
	 */
	public void setType(UnitType type) {
		if (type == null) {
			throw new NullPointerException("type");
		}
		this.type = type;
	}

	/**
	 * Sets the relation.
	 * @param relation the relation to set.
	 */
	public void setRelation(UnitRelation relation) {
		if (relation == null) {
			throw new NullPointerException("relation");
		}
		this.relation = relation;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(389, 37);
		hash.append(loginId);
		hash.append(instanceId);
		hash.append(actionId);
		hash.append(target);
		hash.append(type);
		hash.append(relation);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof AddActionTarget) {
			AddActionTarget compare = (AddActionTarget) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.loginId, compare.loginId);
			equals.append(this.instanceId, compare.instanceId);
			equals.append(this.actionId, compare.actionId);
			equals.append(this.target, compare.target);
			equals.append(this.type, compare.type);
			equals.append(this.relation, compare.relation);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(IAddActionTarget compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.loginId, compare.getLoginId());
		builder.append(this.instanceId, compare.getInstanceId());
		builder.append(this.actionId, compare.getActionId());
		builder.append(this.target, compare.getTarget());
		builder.append(this.type, compare.getType());
		builder.append(this.relation, compare.getRelation());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		string.append("loginId", loginId);
		string.append("instanceId", instanceId);
		string.append("actionId", actionId);
		if (target != null) {
			string.append("target", target);
		}
		if (type != null) {
			string.append("type", type);
		}
		if (relation != null) {
			string.append("relation", relation);
		}
		return string.toString();
	}

	public short getCommandId() {
		return COMMAND_ID;
	}
}
